/***************************************************************************
 *   Copyright (C) 2005 by xihe   *
 *   xihels@163.com                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "ClusterMemberInfoReply.h"


/*
 * =====================================================================================
 *        Class:  GetClusterMemberInfoReplyPacket
 *  Description:  群成员信息的回复包
 * =====================================================================================
 */

GetClusterMemberInfoReplyPacket::GetClusterMemberInfoReplyPacket(
	const uint8_t* buf,
	int len) :
    ClusterOperateReply(buf, len, true)
{
    const uint8_t* cur = buf + QQ_HEADER_LENGTH_CLUSTER_IN;

    cur += readBufDw(clusterId, cur);

    QQBuddy buddy;
    const uint8_t* max = buf + len;
    members.reserve((max - cur) / sizeof (QQBuddy) + 1);

    while (cur < max) {
	cur += buddy.getBody(cur, len);
	members.push_back(buddy);
    }	
    if (cur - buf > len)
	throw runtime_error("GetClusterMemberInfoReplyPacket() Error: BUFFER len not enough");
}

/*
 * =====================================================================================
 *        Class:  GetClusterMemberInfoReplyPacket
 *  Description:  在线群成员的回复包
 * =====================================================================================
 */

GetClusterOnlineMemberReplyPacket::
GetClusterOnlineMemberReplyPacket(const uint8_t* buf, 
	int len) :
    ClusterOperateReply(buf, len, true)
{
    /**
     * 得到在线成员的回复包，格式为：
     * 1. 头部
     * 2. 命令类型，1字节，请求成员信息是0x0B
     * 3. 回复码，1字节，成功是0x0
     * 4. 群内部ID，4字节
     * 5. 未知字节，0x3C
     * 6. 在线成员的qq号
     * 7. 如果有更多在线成员，重复6
     * 8. 尾部
     */
    const uint8_t* cur = buf + QQ_HEADER_LENGTH_CLUSTER_IN;

    cur += readBufDw(clusterId, cur);

    uint32_t uid;
    const uint8_t* max = buf + len;
    cur++;

    members.reserve((max - cur) / sizeof(uint32_t) + 1);
    while (cur < max) {
	cur += readBufDw(uid, cur);
	members.push_back(uid);
    }
    if (cur - buf > len)
	throw runtime_error("GetClusterOnlineMemberReplyPacket() Error: BUFFER len not enough");
}
